﻿#include "usermanagewidget.h"
#include "ui_usermanagewidget.h"
#include <QStandardItemModel>
#include <QDebug>
#include <QInputDialog>
#include "company.h"
#include <QMessageBox>

UserManageWidget::UserManageWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::UserManageWidget)
{
    ui->setupUi(this);

    ui->tableView->setSortingEnabled(true);

    connect(ui->treeView, &UserManageTreeView::queryUserRelations,
            [=]()
    {
        userRelationService.queryUserRelation(
                    ui->tableView,
                    ui->treeView,
                    true,
                    ui->lineEditQueryOverall->text(),
                    ui->lineEditQueryContact->text(),
                    ui->lineEditQueryNickName->text(),
                    ui->lineEditQueryTel->text());
    });

    connect(ui->tableView, &UserManageTableView::refreshUserRelationData,
            [=](double totalTicketIn,
            double totalTicketOut,
            double totalMoneyIn,
            double totalMoneyOut)
    {
        if(widgetType == UserRelation::Client){
            ui->labelTotalTicket->setText("应开发票："+QString::number(totalTicketOut, 'f', 2));
            ui->labelTotalMoney->setText("应收货款："+QString::number(totalMoneyIn, 'f', 2));
        }else{
            ui->labelTotalTicket->setText("应收发票："+QString::number(totalTicketIn, 'f', 2));
            ui->labelTotalMoney->setText("应付货款："+QString::number(totalMoneyOut, 'f', 2));
        }
    });

    connect(ui->tableView, &QTableView::doubleClicked,
            [=](const QModelIndex index)
    {
        Q_UNUSED(index);
        this->on_pushButtonInfo_clicked();
    });

    connect(&userRelationService, &UserRelationService::updateUserRelationDataSuccess,
            [=](QList<UserRelation*> relations)
    {
        ui->tableView->clear();
        ui->tableView->insertUserRelation(relations, true);
    });
}

UserManageWidget::~UserManageWidget()
{
    delete ui;
}

void UserManageWidget::setWidgetType(UserRelation::RelationType widgetType)
{
    this->widgetType = widgetType;
    ui->tableView->setWidgetType(widgetType);
    ui->tableView->setListPushButton(ui->pushButtonList);
    ui->tableView->initHiddenState();
    newUserRelationWidget.setWidgetType(widgetType);
    userRelationInfoWidget.setWidgetType(widgetType);

    if(widgetType == UserRelation::Client){
        ui->labelTypeName->setText("客户管理");
        ui->labelQueryNickName->setText("客户名称");
        ui->pushButtonNew->setText("新客户");
        ui->treeView->setCategoryType(Company::ClientCategory);
    }else{
        ui->labelTypeName->setText("供应商管理");
        ui->labelQueryNickName->setText("供应商名称");
        ui->pushButtonNew->setText("新供应商");
        ui->treeView->setCategoryType(Company::GiverCategory);
    }
}

void UserManageWidget::on_pushButtonNew_clicked()
{
    newUserRelationWidget.setDirectory(ui->treeView->primaryDirectoryList(), ui->treeView->secondaryDirectoryList());
    newUserRelationWidget.setCurrentDirectory(ui->treeView->selectedPrimaryDirectory(), ui->treeView->selectedSecondaryDirectory());
    newUserRelationWidget.showAndClear();
    userRelationService.updateUserRelationData(this, widgetType);
}

void UserManageWidget::on_pushButtonInfo_clicked()
{
    UserRelation *relation = ui->tableView->currentUserRelation();

    if(relation != nullptr){
        userRelationInfoWidget.setDirectory(ui->treeView->primaryDirectoryList(), ui->treeView->secondaryDirectoryList());
        userRelationInfoWidget.initWithUserRelation(relation);
        userRelationInfoWidget.exec();
        userRelationService.updateUserRelationData(this, widgetType);
    }else{
        QString typeStr = this->widgetType==UserRelation::Client?"客户":"供应商";
        QMessageBox::warning(this, "警告", "请先选择"+typeStr+"！");
    }
}

void UserManageWidget::loadCategory()
{
    if(widgetType == UserRelation::Client){
        ui->treeView->initWithCategory(Company::getClientCategory(), "客户类别", "所有客户");
    }else{
        ui->treeView->initWithCategory(Company::getGiverCategory(), "供应商类别", "所有供应商");
    }
}

void UserManageWidget::loadUserRelation()
{
    ui->tableView->loadUserRelations(true);
}

void UserManageWidget::on_pushButtonUpdateData_clicked()
{
    userRelationService.updateUserRelationData(this, widgetType);
}

void UserManageWidget::on_pushButtonQueryClear_clicked()
{
    ui->lineEditQueryOverall->clear();
    ui->lineEditQueryContact->clear();
    ui->lineEditQueryNickName->clear();
    ui->lineEditQueryTel->clear();
}

void UserManageWidget::on_lineEditQueryOverall_textChanged(const QString &text)
{
    userRelationService.queryUserRelation(
                ui->tableView,
                ui->treeView,
                true,
                text,
                ui->lineEditQueryNickName->text(),
                ui->lineEditQueryContact->text(),
                ui->lineEditQueryTel->text());
}

void UserManageWidget::on_lineEditQueryContact_textChanged(const QString &text)
{
    userRelationService.queryUserRelation(
                ui->tableView,
                ui->treeView,
                true,
                ui->lineEditQueryOverall->text(),
                ui->lineEditQueryNickName->text(),
                text,
                ui->lineEditQueryTel->text());
}

void UserManageWidget::on_lineEditQueryNickName_textChanged(const QString &text)
{
    userRelationService.queryUserRelation(
                ui->tableView,
                ui->treeView,
                true,
                ui->lineEditQueryOverall->text(),
                text,
                ui->lineEditQueryContact->text(),
                ui->lineEditQueryTel->text());
}

void UserManageWidget::on_lineEditQueryTel_textChanged(const QString &text)
{
    userRelationService.queryUserRelation(
                ui->tableView,
                ui->treeView,
                true,
                ui->lineEditQueryOverall->text(),
                ui->lineEditQueryNickName->text(),
                ui->lineEditQueryContact->text(),
                text);
}
